package operation.action;

import operation.action.Goods_Operation;
import uesr.Goods;

import java.util.LinkedList;
import java.util.Scanner;

public class RemoveGoods implements Goods_Operation {
    @Override
    public void work(LinkedList<Goods> goods) {
        System.out.println("请选择删除方式");
        System.out.println("1 按名字删除   2 按价格删除");
        Scanner scanner = new Scanner(System.in);
        int choice = scanner.nextInt();

        while (true) {
            if (choice == 1) {
                remoneName(goods);
                break;
            } else if (choice == 2) {
                removePrice(goods);
                break;
            } else {
                System.out.println("选择错误,请重新选择");
                choice = scanner.nextInt();
            }
        }

    }

    public void remoneName (LinkedList<Goods> goods) {
        System.out.println("请输入你要删除的商品的名字");
        Scanner scanner = new Scanner(System.in);
        String name = scanner.nextLine();
        Goods[] tmp = (Goods[])goods.toArray();
        int i = 0;
        for (i = 0; i < goods.size();i++) {
            if (tmp[i].name.equals(name)) {
                System.out.println(tmp[i].name + tmp[i].price + tmp[i].count);
                System.out.println("确定删除");
                goods.remove(tmp[i]);
                System.out.println("删除成功");
            }
        }
        if (i == goods.size()) {
            System.out.println("查无此商品");
        }

    }

    public void removePrice (LinkedList<Goods> goods) {
        System.out.println("请输入你要删除的商品的价格");
        Scanner scanner = new Scanner(System.in);
        double price = scanner.nextDouble();
        Goods[] tmp = (Goods[])goods.toArray();
        int i = 0;
        for (i = 0; i < goods.size();i++) {
            if (tmp[i].price == price) {
                System.out.println(tmp[i].name + tmp[i].price + tmp[i].count);
                System.out.println("确定删除");
                goods.remove(tmp[i]);
                System.out.println("删除成功");
            }
        }
        if (i == goods.size()) {
            System.out.println("查无此商品");
        }
    }

}
